package com.learn.xueer.ui.fragment

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.widget.ImageView
import android.widget.TextView
import androidx.core.view.get
import androidx.core.view.isVisible
import androidx.navigation.fragment.findNavController
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestOptions
import com.learn.xueer.R
import com.learn.xueer.app.base.BaseFragment1
import com.learn.xueer.app.util.CacheUtil
import com.learn.xueer.data.model.bean.PkResponse
import com.learn.xueer.data.model.bean.QuestionBean
import com.learn.xueer.databinding.FragmentPkResultBinding
import com.learn.xueer.ui.viewmodel.PkResultViewModel
import kotlinx.android.synthetic.main.fragment_pk_result.btnNext
import kotlinx.android.synthetic.main.fragment_pk_result.iv_avatar_user_first
import kotlinx.android.synthetic.main.fragment_pk_result.iv_avatar_user_second
import kotlinx.android.synthetic.main.fragment_pk_result.iv_first_win
import kotlinx.android.synthetic.main.fragment_pk_result.iv_second_win
import kotlinx.android.synthetic.main.fragment_pk_result.iv_status_img
import kotlinx.android.synthetic.main.fragment_pk_result.iv_status_text
import kotlinx.android.synthetic.main.fragment_pk_result.ll_words
import kotlinx.android.synthetic.main.fragment_pk_result.tv_name_user_first
import kotlinx.android.synthetic.main.fragment_pk_result.tv_name_user_second
import kotlinx.android.synthetic.main.fragment_pk_result.tv_score
import kotlinx.android.synthetic.main.fragment_pk_result.tv_time
import java.util.Random


class PkResultFragment : BaseFragment1<PkResultViewModel, FragmentPkResultBinding>() {


    @SuppressLint("SetTextI18n")
    override fun initView(savedInstanceState: Bundle?) {
        val questions = arguments?.getParcelableArrayList<QuestionBean>("question")
        val pkInfo = arguments?.get("pkInfo") as PkResponse
        val interval = arguments?.get("interval") as Int
        val random = Random()
        val second = Math.max(10500, interval - 5000 + random.nextInt(10000))
        tv_time.text =
            "${interval / 1000}.${interval % 1000 / 100}s : ${second / 1000}.${second % 1000 / 100}s"
        CacheUtil.getUser()?.let {
            tv_name_user_first.text = it.nickname
            Glide.with(iv_avatar_user_first)
                .load(it.avatar)
                .apply(RequestOptions.bitmapTransform(CircleCrop()))
                .transition(DrawableTransitionOptions.withCrossFade(500))
                .into(iv_avatar_user_first)
        }
        tv_name_user_second.text = pkInfo.otherUser.nickname
        Glide.with(iv_avatar_user_second)
            .load(pkInfo.otherUser.avatar)
            .apply(RequestOptions.bitmapTransform(CircleCrop()))
            .transition(DrawableTransitionOptions.withCrossFade(500))
            .into(iv_avatar_user_second)
        var scoreFirst = 0
        var scoreSecond = 0

        tv_score.text = "${scoreFirst} - ${scoreSecond}"
        iv_first_win.isVisible = scoreFirst >= scoreSecond
        iv_second_win.isVisible = scoreFirst <= scoreSecond
        pkInfo.wordsList.forEach {
            val item = LayoutInflater.from(requireContext())
                .inflate(R.layout.pk_item_result, ll_words, false)
            item.findViewById<TextView>(R.id.tv_word).text = it.words
            item.findViewById<TextView>(R.id.tv_pretations).text = it.pretations
            ll_words.addView(item, ll_words.childCount - 1)
        }
        questions?.let {
            for (i in 0 until questions.size) {
                val itemView = ll_words.get(i)
                val question = questions[i]
                if (question.hasAnswer == 1) {
                    scoreFirst += 1
                    itemView.findViewById<ImageView>(R.id.iv_first_is_correct)
                        .setImageResource(R.mipmap.answer_correct)
                }
                if (question.otherAnswer == 1) {
                    scoreSecond += 1
                    itemView.findViewById<ImageView>(R.id.iv_second_is_correct)
                        .setImageResource(R.mipmap.answer_correct)
                }
            }
        }

        if (scoreFirst >= scoreSecond) {
            iv_status_img.setImageResource(R.mipmap.result_win_img)
            iv_status_text.setImageResource(R.mipmap.result_win_text)
            iv_first_win.isVisible = true
            iv_second_win.isVisible = false
        } else {
            iv_status_img.setImageResource(R.mipmap.result_lose_img)
            iv_status_text.setImageResource(R.mipmap.result_lose_text)
            iv_first_win.isVisible = false
            iv_second_win.isVisible = true
        }
        tv_score.text = "$scoreFirst - $scoreSecond"
        mViewModel.savePk(scoreFirst >= scoreSecond)
        btnNext.setOnClickListener {
            findNavController().navigate(R.id.again)
        }
    }
}